Conditions | 16 |
Paths | 13 |
Total Lines | 99 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 16 |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like floatify.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 'use strict'; |
||
70 | 279 | var parse = function parse(str) { |
|
71 | 279 | var string = str; |
|
72 | var spacePos; |
||
73 | var spaceSplit; |
||
74 | var spaceCount; |
||
75 | var dotPos; |
||
76 | var commaPos; |
||
77 | var lDotPos; |
||
78 | var lCommaPos; |
||
79 | var dotCount; |
||
80 | var commaCount; |
||
81 | |||
82 | function parseMixedSeparators() { |
||
83 | // format is using dot and comma |
||
84 | |||
85 | // last dot position |
||
86 | 68 | lDotPos = string.lastIndexOf('.'); |
|
87 | // last comma position |
||
88 | 68 | lCommaPos = string.lastIndexOf(','); |
|
89 | |||
90 | // order of 1st dot -> comma must be same as last dot -> comma |
||
91 | // 123.123.123,123 -> ok 123.123,123.123 -> not ok |
||
92 | 68 | if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) { |
|
93 | 3 | return Number.NaN; |
|
94 | } |
||
95 | |||
96 | // check positions to guess the thousands separator |
||
97 | 65 | if (dotPos > commaPos) { |
|
98 | 57 | if (dotCount > 1) { |
|
99 | 1 | return Number.NaN; |
|
100 | } |
||
101 | // best guess: . is thousands separator and , is decimal point |
||
102 | 56 | return toFloatFormat(string, ',', '.'); |
|
103 | } |
||
104 | |||
105 | 8 | if (commaCount > 1) { |
|
106 | 1 | return Number.NaN; |
|
107 | } |
||
108 | // best guess: , is thousands separator and . is decimal point |
||
109 | 7 | return toFloatFormat(string, '.', ','); |
|
110 | } |
||
111 | |||
112 | 279 | string = string.trim(); |
|
113 | |||
114 | // 1st dot position |
||
115 | 279 | dotPos = string.indexOf('.'); |
|
116 | // 1st comma position |
||
117 | 279 | commaPos = string.indexOf(','); |
|
118 | // 1st space position |
||
119 | 279 | spacePos = string.indexOf(' '); |
|
120 | |||
121 | 279 | if (dotPos + commaPos + spacePos === -3) { |
|
122 | // life is good, no separators |
||
123 | 18 | return toFloatFormat(string); |
|
124 | } |
||
125 | |||
126 | 261 | spaceSplit = string.split(' '); |
|
127 | 261 | spaceCount = spaceSplit.length - 1; |
|
128 | 261 | dotCount = string.split('.').length - 1; |
|
129 | 261 | commaCount = string.split(',').length - 1; |
|
130 | |||
131 | // only combination of 2 separators allowed |
||
132 | 261 | if (dotCount > 0 && commaCount > 0 && spaceCount > 0) { |
|
133 | 1 | return Number.NaN; |
|
134 | } |
||
135 | |||
136 | // if there is any separator (space, comma, dot) found more than once, |
||
137 | // all other must not be found more than once |
||
138 | 260 | if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) { |
|
139 | 4 | return Number.NaN; |
|
140 | } |
||
141 | |||
142 | 256 | if (commaCount > 1 && spaceCount > 1) { |
|
143 | 1 | return Number.NaN; |
|
144 | } |
||
145 | |||
146 | 255 | if (spaceCount > 0) { |
|
147 | 72 | if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) { |
|
148 | 17 | return Number.NaN; |
|
149 | } |
||
150 | 55 | string = spaceSplit.join(''); |
|
151 | } |
||
152 | |||
153 | 238 | if (dotPos !== -1 && commaPos !== -1) { |
|
154 | 68 | return parseMixedSeparators(); |
|
155 | } |
||
156 | |||
157 | 170 | if (dotPos !== -1) { |
|
158 | // only dot(s) in format |
||
159 | 85 | return parseParts(string, '.', dotCount); |
|
160 | } |
||
161 | |||
162 | 85 | if (commaPos !== -1) { |
|
163 | // only comma(s) in format |
||
164 | 80 | return parseParts(string, ',', commaCount); |
|
165 | } |
||
166 | |||
167 | 5 | return toFloatFormat(string); |
|
168 | }; |
||
169 | |||
178 |